package com.yl.chainStore.entity.customer


import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableField
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import com.yl.chainStore.common.enums.TypeEnum
import com.yl.chainStore.entity.OtherShow
import com.yl.chainStore.entity.pay.PayItemCustomerShowRule
import com.yl.chainStore.util.getAge
import com.yl.common.annotation.NoArg
import com.yl.common.getContext
import com.yl.users.common.enum.CustomerSourceType
import java.util.*

/**
 * <p>
 *
 * </p>
 *
 * @author cxw
 * @since 2021-06-25
 */
@NoArg
@TableName(value = "customer_info", autoResultMap = true)
data class CustomerInfo(

	/**
	 * 公司客户表
	 */
	@TableId(value = "id", type = IdType.AUTO)
	var id: Long,

	/**
	 * 姓名
	 */
	@TableField("name")
	var name: String,

	/**
	 * 性别
	 */
	@TableField("gender")
	var gender: TypeEnum = TypeEnum.OTHER,

	/**
	 * 公司id
	 */
	@TableField("company_id")
	var companyId: Long = getContext().companyId,

	/**
	 * 创建用户
	 */
	@TableField("create_user_id")
	var createUserId: Long = getContext().userId,

	/**
	 * 创建用户名
	 */
	@TableField(exist = false)
	var createUserName: String? = null,

	/**
	 * 创建时间
	 */
	@TableField("create_time")
	var createTime: Date = Date(),

	/**
	 * 最后修改人
	 */
	@TableField("update_user_id")
	var updateUserId: Long = getContext().userId,

	/**
	 * 最后修改时间
	 */
	@TableField("update_time")
	var updateTime: Date = Date(),

	/**
	 * 数据有效性
	 */
	@TableField("enabled")
	var enabled: Int = 1,


	/**
	 * 客户类型
	 */
	@TableField("type")
	var type: Long? = null,

	@TableField(exist = false)
	var typeName: String? = null,

	/**
	 * 头像路径
	 */
	@TableField("avatar_url")
	var avatarUrl: String? = null,

	/**
	 * 病历号码
	 */
	@TableField("history_sick_num")
	var historySickNum: String,

	/**
	 * 生日
	 */
	@TableField("birth_date")
	var birthDate: Date? = null,

	/**
	 * 昵称
	 */
	@TableField("nickname")
	var nickname: String? = null,

	/**
	 * 证件类型
	 */
	@TableField("certificate_type")
	var certificateType: TypeEnum? = TypeEnum.USER_CARD_TYPE_ID_CARD,

	/**
	 * 证件号码
	 */
	@TableField("certificate_num")
	var certificateNum: String? = null,

	/**
	 * 客户来源类型
	 */
	@TableField("source_type")
	var sourceType: CustomerSourceType? = CustomerSourceType.DEFAULT,


	/**
	 * 来源备注
	 */
	@TableField("source_remark")
	var sourceRemark: String? = null,

	/**
	 * 来源标签
	 */
	@TableField("source_label")
	var sourceLabel: String? = null,


	/**
	 * QQ
	 */
	@TableField("qq_account")
	var qqAccount: String? = null,

	/**
	 * 微信
	 */
	@TableField("wechat_account")
	var wechatAccount: String? = null,

	/**
	 * 手机
	 */
	@TableField(exist = false)
	var mobileNum: List<CustomerInfoPhone>? = null,
	/**
	 * 全部手机显示
	 */
	@TableField(exist = false)
	var mobileNumStr: String? = null,

	/**
	 * 只显示第一个
	 */
	@TableField(exist = false)
	var mobileFirstNum: String? = null,

	/**
	 * 只显示第一个 带关系
	 */
	@TableField(exist = false)
	var mobileFirstNumStr: String? = null,

	/**
	 * 电话
	 */
	@TableField(exist = false)
	var phone: List<CustomerInfoPhone>? = null,

	/**
	 * 全部手机显示
	 */
	@TableField(exist = false)
	var phoneNumStr: String? = null,

	/**
	 * 省
	 */
	@TableField("province")
	var province: String? = null,

	/**
	 * 市
	 */
	@TableField("city_name")
	var cityName: String? = null,

	/**
	 * 县
	 */
	@TableField("county_name")
	var countyName: String? = null,

	/**
	 * 详细地址
	 */
	@TableField("address_detail")
	var addressDetail: String? = null,

	/**
	 * 邮箱
	 */
	@TableField("email")
	var email: String? = null,

	@TableField("health_care_card_number")
	var healthCareCardNumber: String? = null,

	@TableField("remark")
	var remark: String? = null,


	/**
	 * 患者一级来源
	 */
	@TableField("first_source")
	var firstSource: String? = null,

	/**
	 * 患者二级来源
	 */
	@TableField("second_source_id")
	var secondSourceId: Long? = null,

	@TableField(exist = false)
	var secondSource: String? = null,

	/**
	 * 内部推广人
	 */
	@TableField("source_company_user_id")
	var sourceCompanyUserId: Long? = null,

	@TableField(exist = false)
	var sourceCompanyUserName: String? = null,

	/**
	 * 客户推广人
	 */
	@TableField("source_customer_id")
	var sourceCustomerId: Long? = null,

	@TableField(exist = false)
	var sourceCustomerUserName: String? = null,

	/**
	 * 三级来源字典id
	 */
	@TableField("source_dist_id")
	var sourceDistId: Long? = null,

	@TableField(exist = false)
	var sourceDistValue: String? = null,

	/**
	 * 最后就诊记录（便于查询）
	 */
	@TableField("last_record_detail_id")
	var lastRecordDetailId:Long?=null,

	@TableField(exist = false)
	var lastDoctorId:Long?=null,

	@TableField(exist = false)
	var lastDoctorName:String?=null,



	/**
	 * 订单项目显示规则
	 */
	@TableField(exist = false)
	var showRules: List<PayItemCustomerShowRule>? = null,

	/**
	 * 是否欠款
	 */
	@TableField(exist = false)
	var hasArrearsOrder: Boolean? = null,

	/**
	 * 最后就诊医生
	 */
	@TableField(exist = false)
	var lastRecordDetailDoctorName: String? = null,

	/**
	 * 最后就诊咨询师
	 */
	@TableField(exist = false)
	var lastSecondaryUserName: String? = null,

	/**
	 * 最后就诊时间
	 */
	@TableField(exist = false)
	var lastSickRecordTime: Date? = null,

	/**
	 * 最后就诊记录所属分院
	 */
	@TableField(exist = false)
	var lastSickRecordSubCompanyName:String ?= null,



	/**
	 * 推荐患者数
	 */
	@TableField(exist = false)
	var recommendedCustomerCount: Int? = null,


	) : OtherShow() {


	val age: Int?
		get() {
			if (birthDate != null) {
				return getAge(birthDate!!)
			}
			return null
		}

	val thirdSourceShow: String?
		get() {
			return when {
				sourceCompanyUserName != null -> {
					sourceCompanyUserName
				}
				sourceCustomerUserName != null -> {
					sourceCustomerUserName
				}
				sourceDistValue != null -> {
					sourceDistValue
				}
				else -> {
					null
				}
			}
		}

	val scoreShow: String
		get() {
			val str = StringBuffer()
			if (firstSource != null) {
				str.append(firstSource)
				if (secondSource != null) {
					str.append("|$secondSource")
					if (thirdSourceShow != null) {
						str.append("|$thirdSourceShow")
					}
				}
			}
			return str.toString()
		}

	/**
	 * 下面2个保存来源的时候用
	 * 上面java 获取前端参数用
	 * 下面html 回显用
	 */
	@TableField(exist = false)
	var sourceInsert: String? = null

	@TableField(exist = false)
	var sourceRes: String? = null
		get() {
			if (sourceInsert != null && sourceInsert!!.isNotEmpty()) {
				return sourceInsert
			}
			if (firstSource != null && firstSource!!.isNotEmpty()) {
				var res = firstSource
				if (secondSourceId != null) {
					res = "$res|$secondSourceId"
					if (sourceCompanyUserId != null) {
						res = "$res|$sourceCompanyUserId"
					} else if (sourceCustomerId != null) {
						res = "$res|$sourceCustomerId"
					} else {
						if (sourceDistId != null) {
							res = "$res|$sourceDistId"
						}
					}
				}
				return res
			}
			return null
		}
		set(value) {
			sourceInsert = value
			field = value
		}


}
